package com.example.suanfademo;

import com.sun.source.tree.WhileLoopTree;

public class ArrayMerging {
    /*
    两个有序数组合并，数组分别为nums1,nums2
    m是数组1中有效元素的长度   n是数组2中有效元素的数组长度
    [1,2,5,0,0,0]  [2,3,4]  m=3,n=3作为输入
    输出为[1,2,2,3,4,5]
     */
    public int[] arrayMerg(int[] nums1,int[]nums2,int m,int n){
        int num1Index = m-1;
        int num2Index = n-1;
        int index = nums1.length - 1;
        while (num1Index>=0 && num2Index>=0){
            if (nums1[num1Index]>= nums2[num2Index]) {
                nums1[index] = nums1[num1Index];
                num1Index--;
            }else {
                nums1[index] = nums2[num2Index];
                num2Index--;
            }
            index--;
        }
        if(num2Index>=0){
            while (num2Index>=0){
                nums1[index] = nums2[num2Index];
                index--;
                num2Index--;
            }
        }
        System.out.println(nums1);
        return nums1;
    }

    public static void main(String[] args) {
        ArrayMerging arrayMerging = new ArrayMerging();
        int[] nums1 = new int[]{7,8,9,0,0,0};
        int[] nums2 = new int[]{2,3,5};
        int m = 3;
        int n = 3;
        int[] ints = arrayMerging.arrayMerg(nums1, nums2, m, n);
        for(int i=0; i<ints.length;i++){
            System.out.println(ints[i]);
        }
    }
}
